﻿using MobiSms.DataModel;
using MobiSms.Library;
using MobiSms.Library.Extensions;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace MobiSms.ViewModel
{
    public class MessageDetailsViewModel : MainViewModel
    {
        private string _guid;
        public string Guid
        {
            get { return _guid; }
            set
            {
                _guid = value;
                RaisePropertyChanged(() => Guid);
                if (!IsDesignMode)
                {
                    if (Guid != null && Guid != string.Empty)
                    {
                        GetMessageBodyInfo(Guid);
                    }
                }
            }
        }

        private MessageInfo _selectedMessage;
        public MessageInfo SelectedMessage
        {
            get { return _selectedMessage; }
            set
            {
                _selectedMessage = value;
                RaisePropertyChanged(() => SelectedMessage);
                RaisePropertyChanged(() => DeleteCommand);
            }
        }

        private ObservableCollection<MessageMobileInfo> _itemsOfMobile;
        public ObservableCollection<MessageMobileInfo> ItemsOfMobile
        {
            get
            {
                return _itemsOfMobile;
            }
            set
            {
                _itemsOfMobile = value;
                RaisePropertyChanged(() => ItemsOfMobile);
            }
        }

        private void GetMessageBodyInfo(string guid)
        {
            Deployment.Current.Dispatcher.BeginInvoke(() =>
            {
                SelectedMessage = Database.CollectionOfMessages.FirstOrDefault(m => m.Guid == guid);
                if (SelectedMessage != null)
                {
                    GetMobilesCollection(SelectedMessage.Guid);
                }
                else
                {
                    NavigateBack();
                }
            });
        }

        private void GetMobilesCollection(string messageGuid)
        {
            IEnumerable<MessageMobileInfo> items = Database.CollectionOfMobiles.Where(m => m.MessageGuid == messageGuid);
            ItemsOfMobile = new ObservableCollection<MessageMobileInfo>(items);
        }

        private void Remove(MessageInfo entity)
        {
            Database.CollectionOfMobiles.DeleteAllOnSubmit(Database.CollectionOfMobiles.Where(m => m.MessageGuid == entity.Guid));

            Database.CollectionOfMessages.DeleteOnSubmit(entity);

            Submit();

            NavigateBack();
        }

        public ICommand DeleteCommand
        {
            get
            {
                return new GenericCommand()
                {
                    ExecuteCallback = delegate {
                        if (MessageBox.Show(Application.Current.FindResource<string>(LanguageKeys.IDS_NOTIFICATION_DELETE_COMFIRM_BODY), Application.Current.FindResource<string>(LanguageKeys.IDS_NOTIFICATION_DELETE_COMFIRM_TITLE), MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                        {
                            Remove(SelectedMessage);
                        }
                    }
                };
            }
        }
    }
}
